package com.curvebreakers.wheresmystuff.activities;

import com.curvebreakers.wheresmystuff.R;
import com.curvebreakers.wheresmystuff.model.*;

import android.os.*;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.*;
import android.widget.*;

public class RegistrationFragment extends Fragment implements DBResultListener {
	private EditText email, password, confirmPassword, phoneNum;
	
    private ProgressDialog progressDialog;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_registration, container, false);
		
		email = (EditText) view.findViewById(R.id.regEmail);
	    password = (EditText) view.findViewById(R.id.regPassword);
	    confirmPassword = (EditText) view.findViewById(R.id.regConfirmPassword);
	    phoneNum = (EditText) view.findViewById(R.id.regPhoneNum);
	    
	    final DBResultListener listener = this;
	    final Button button = (Button) view.findViewById(R.id.regSubmitButton);
        button.setOnClickListener(new View.OnClickListener() {           
	        public void onClick(View v) {
	        	// Validate the form
	        	boolean valid = true;
	        	
	        	if(!email.getText().toString().matches("^(?i)[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$")) {
	        		email.setError("Please enter a valid email");
	        		valid = false;
	        	}
	        	
	        	if (password.getText().toString().length() < 8) {
	        		password.setError("Passwords must be at least 8 character");
	        		valid = false;
	        	} else if (!password.getText().toString().equals(confirmPassword.getText().toString())) {
	        		confirmPassword.setError("Passwords do not match");
	        		valid = false;
	        	}
	        	
	        	if (valid) {
		            progressDialog = new ProgressDialog(getActivity(), ProgressDialog.THEME_TRADITIONAL);//ProgressDialog.show(getActivity(), "Validating login...", "Please Wait");
		            progressDialog.setTitle("Creating your account...");
		            progressDialog.setMessage("Please Wait");
		            progressDialog.show();
		        	User u = new User(email.getText().toString(), password.getText().toString(), phoneNum.getText().toString());
		            DatabaseModel.saveUser(u, listener);
	            }
	        }
        });
        return view;
	}
	
	@Override
	public void onDBResultReceived(Object obj) {
		progressDialog.dismiss();
		Session.getSession().login((User)obj);
		Intent goToNextActivity = new Intent(getActivity(), HomeActivity.class);
		goToNextActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(goToNextActivity);
	}

	@Override
	public void onDBRequestFailure(String message) {
		progressDialog.dismiss();
		Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
	}

}
